<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<link href="style/style.css" rel="stylesheet" type="text/css">
<link href="style/global.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="<%=basePath%>/js/global.js"></script>
<jsp:include page="/extInclude.jsp" />
<script type="text/javascript">
var corverRights = function(arr) {
    var tmp = [];
    Ext.each(arr.split(','), function(val) {
                tmp.push('r' + val)
            });
    return tmp.join();
}
var modids = '${role.modids}', rightids = corverRights('${role.rightids}');
var data = (modids + "," + rightids).split(',');
Ext.onReady(function() {
    var loaddata = function(data) {
        setValue(treeMenu, data);
        treeMenu.addListener('checkchange', checkchange);
    }
    var rightLoader = new Ext.tree.TreeLoader({
                id : 'rightLoader',
                url : _ctxPath + '/listAllRight.do'
            });
    var checkchange = function(node, checked) {
        // treeRight.root.setId(node.id);
        // treeRight.root.reload();
        var flg = false;
        // if (!node.isLeaf()) {
        if (checked) {
            node.parentNode.getUI().toggleCheck(true);
            node.eachChild(function(n) {
                        if (n.getUI().isChecked()) {
                            flg = true;
                            return;
                        }
                    });
            if (flg == false) {
                node.eachChild(function(n) {
                            if (n.getUI().isChecked() == false) {
                                n.getUI().toggleCheck(true)
                            }
                        });
            }
            node.getUI().addClass('x-tree-node-checked');
        } else {
            node.parentNode.eachChild(function(n) {
                        if (n.getUI().isChecked()) {
                            flg = true;
                            return;
                        }
                    })
            if (flg == false) {
                node.parentNode.getUI().toggleCheck(false)
            }
            node.getUI().removeClass('x-tree-node-checked');
            node.eachChild(function(n) {
                        if (n.getUI().isChecked()) {
                            flg = true;
                            return;
                        }
                    });
            if (flg == true) {
                node.eachChild(function(n) {
                            if (n.getUI().isChecked()) {
                                n.getUI().toggleCheck(false)
                            }
                        });
            }
        }
    };
    // load the has the value setttings.
    var convertValue = function(val) {
        // init return array
        var a = [];

        // calls itself recursively if necessary
        if (1 < arguments.length) {
            for (var i = 0; i < arguments.length; i++) {
                a.push(this.convertValue(arguments[i]));
            }
        }

        // nothing to do for arrays
        else if (Ext.isArray(val)) {
            a = val;
        }

        // just push numbers
        else if ('number' === typeof val) {
            a.push(val);
        }

        // split strings
        else if ('string' === typeof val) {
            a = val.split(this.separator);
        }
        return a;
    }

    var setChecked = function(checked) {
        checked = true === checked ? checked : false;
        var cb = this.cbEl || false;
        if (cb) {
            true === checked ? cb.addClass('x-tree-node-checked') : cb
                    .removeClass('x-tree-node-checked');
        }
        this.node.attributes.checked = checked;
        this.onCheckChange();
        return checked;
    }

    var setValue = function(obj, val) {
        var node = null;
        Ext.each(val, function(id) {
                    if (Ext.isNumber(id * 1)) {
                        node = obj.nodeHash[id];
                    } else {
                        obj.getRootNode().cascade(function(n) {
                                    // if (!n.isLeaf() && !n.isLoaded()) {
                                    // n.reload();
                                    // return true;
                                    // }
                                    if (n.attributes['code'] == id) {
                                        node = n;
                                        return false;
                                    }
                                    return true;
                                });
                    }

                    if (node) {
                        var ui = node.getUI();
                        ui.toggleCheck(true);
                        node.expand();
                    }
                }, obj);

        return obj.value;
    }
    var treeMenu = new Ext.tree.TreePanel({
        id : 'treeMenu',
        renderTo : 'right-tree',
        title : "为【${role.name}】授权",
        height : 400,
        width : 400,
        useArrows : true,
        autoScroll : true,
        containerScroll : true,
        rootVisible : false,
        frame : true,
        root : {
            id : -1,
            checked : true,
            nodeType : 'async'
        },
        dataUrl : 'listAllModule.do',
        tbar : ['-', {
        			iconCls:'btn-refresh',
                    text : '刷新',
                    handler : function() {
                        treeMenu.getRootNode().reload();
                        Ext.Msg.alert('操作信息', '已刷新');
                    }
                }, '-', {
                	iconCls:'btn-save',
                    text : '保存',
                    handler : function() {
                        modids = [];
                        rightids = [];
                        var selNodes = treeMenu.getChecked();
                        Ext.each(selNodes, function(node) {
                                    if (node.attributes['code']) {
                                        rightids.push(node.attributes['code']
                                                .replace('r', ''));
                                    }
                                    if (Ext.isNumber(node.id*1)) {
                                        modids.push(node.id);
                                    }
                                });
                               // alert(modids.join()+"---------"+rightids.join())
                               // return;
                        Ext.Ajax.request({
                            url : 'saveRole.do',
                            method : 'POST',
                            params : {
                                op : 'ajax',
                                'role.id' : ${role.id},
                                'role.modids' : modids.join(),
                                'role.rightids' : rightids.join()
                            },
                            success : function(response, options) {
                                var res = Ext.util.JSON
                                        .decode(response.responseText);
                                if (res.success) {
                                    Ext.Msg.alert('操作信息', '保存成功！');
                                    //treeMenu.root.reload();
                                    //data = (modids + "," + corverRights(rightids.join())).split(',');
                                    //loaddata(data);
                                } else {
                                    Ext.Msg.alert('操作信息', '保存失败！');
                                }

                            },
                            failure : function(response, options) {
                                Ext.Msg.alert('操作信息', '保存失败！');
                            },
                            scope : this
                        });
                    }
                }, '-'],
        listeners : {
            click : function(node, e) {
                var flg = node.getUI().isChecked();
                if (flg == false) {
                    node.getUI().toggleCheck(true)
                } else {
                    node.getUI().toggleCheck(false)
                }
            },
            load : function() {
                treeMenu.removeListener('checkchange', checkchange);
                loaddata(data);
            },
            append : function(t, p, n, i) {
                if (n.attributes['flg']) {
                    n.loader = rightLoader;
                }
            }
        }
    });
})
</script>
<div class="rightCon" align="center">
	<div class="rightTitle" align="left" style="width: 400px;">
		<h3>
			<s:property value="nav" escape="false"/>
		</h3>
	</div>
	<table>
		<tr>
			<td>
				<div id="right-tree"></div>
			</td>
		</tr>
	</table>
</div>